package com.mike.fragmentdemo3.Fragment.Fragment;

import static android.content.Context.MODE_PRIVATE;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.mike.fragmentdemo3.R;

/**
 * Created by Jay on 2015/8/28 0028.
 */
public class PatternFragment extends Fragment {
    private ImageView cartoonComingHomeIv;
    private ImageView ComingHomeIv;
    private ImageView mLeavingHomeIv;
    private ImageView mCartoonLeavingHomeIv;
    private ImageView mSleepModeIv;
    private ImageView mCartoonSleepModeIv;
    private ImageView mCartoonEnergySavingIv;
    private ImageView mEnergySavingIV;
    private ImageView mSleepIv;
    private ImageView mComingHomeIv;
    private ImageView mLeavingHomeIV;
    private ImageView mEnergySavingIv;
    private ImageView mLocationIv;
    private ImageView mComingHomeWaringIv;

    private ImageView mSleepModeWarningIv;
    private ImageView mLeavingHomeWarningIv;
    private TextView mSleepModeDevicesTv;
    private TextView mComingHomeDevicesTv;
    private TextView mLeavingHomeDevicesTv;
    private TextView mEnergySavingDevicesTv;

    private boolean mAcState;
    private boolean mSoState;
    private boolean mApState;
    private boolean mHuState;//0关闭，1开启
    private boolean mAcOn;
    private boolean mSoOn;
    private boolean mApOn;
    private boolean mHuOn;
    private boolean mAcWaring;
    private boolean mSoWaring;
    private boolean mApWaring;
    private boolean mHuWaring;

    private int mFlagComingHome = 1;
    private int mFlagLeavingHome = 1;
    private int mFlagSleepMode = 1;
    private int mFlagEnergySaving = 1;
    private boolean mIsOpen = true;
    private Context context;
    private SharedPreferences.OnSharedPreferenceChangeListener prefListener;

    private void updateDeviceCount() {
        SharedPreferences sp = getActivity().getSharedPreferences("deviceInfo", MODE_PRIVATE);
        mAcOn = sp.getBoolean("acState", false);
        mSoOn = sp.getBoolean("soState", false);
        mApOn = sp.getBoolean("apState", false);
        mHuOn = sp.getBoolean("huState", false);
        mAcWaring = sp.getBoolean("acWaring", false);
        mSoWaring = sp.getBoolean("mSoWaring", false);
        mApWaring = sp.getBoolean("mApWaring", false);
        mHuWaring = sp.getBoolean("mHuWaring", false);

        int n = (mAcOn ? 1 : 0) + (mSoOn ? 1 : 0) + (mApOn ? 1 : 0) + (mHuOn ? 1 : 0);
        mSleepModeDevicesTv.setText(n + " Devices");
        mComingHomeDevicesTv.setText(n + " Devices");
        mLeavingHomeDevicesTv.setText(n + " Devices");
        mEnergySavingDevicesTv.setText(n+"Devices");

        if (mAcOn == true && mSoOn == true && mApOn == true && mHuOn == true) {
            ComingHomeIv.setBackgroundResource(R.drawable.shape_bg_background2);
            mComingHomeIv.setBackgroundResource(R.drawable.zaijia_on);
            mIsOpen = false;
        } else {
            ComingHomeIv.setBackgroundResource(R.drawable.shape_bg_background);
            mComingHomeIv.setBackgroundResource(R.drawable.zaijia_off);
            mIsOpen = true;
        }
        if (mAcOn == false && mSoOn == true && mApOn == false && mHuOn == false) {
            mLeavingHomeIv.setBackgroundResource(R.drawable.shape_bg_background2);
            mLeavingHomeIV.setBackgroundResource(R.drawable.lijia_on);
            mIsOpen = false;
        } else {
            mLeavingHomeIv.setBackgroundResource(R.drawable.shape_bg_background);
            mLeavingHomeIV.setBackgroundResource(R.drawable.lijia_off);
            mIsOpen = true;
        }
        if (mAcOn == false && mSoOn == false && mApOn == false && mHuOn == false) {
            mSleepIv.setBackgroundResource(R.drawable.sleep_on);
            mSleepModeIv.setBackgroundResource(R.drawable.shape_bg_background2);
            mIsOpen = false;
        } else {
            mSleepModeIv.setBackgroundResource(R.drawable.shape_bg_background);
            mSleepIv.setBackgroundResource(R.drawable.sleep_off);
            mIsOpen = true;
        }
        if (mAcOn == false && mSoOn == true && mApOn == false && mHuOn == true) {
            mEnergySavingIv.setBackgroundResource(R.drawable.jieneng_on);
            mEnergySavingIV.setBackgroundResource(R.drawable.shape_bg_background2);
            mIsOpen = false;
        } else {
            mEnergySavingIV.setBackgroundResource(R.drawable.shape_bg_background);
            mEnergySavingIv.setBackgroundResource(R.drawable.jieneng_off);
            mIsOpen = true;
        }

        if (mAcWaring == true || mSoWaring == true || mApWaring == true || mHuWaring == true) {
            ComingHomeIv.setBackgroundResource(R.drawable.shape_bg_background_waring);
            mComingHomeIv.setBackgroundResource(R.drawable.zaijia_off);
            mComingHomeDevicesTv.setText("Not Available");
            mComingHomeWaringIv.setVisibility(View.VISIBLE);
        }
        if (mSoWaring == true) {
            mLeavingHomeIv.setBackgroundResource(R.drawable.shape_bg_background_waring);
            mLeavingHomeIV.setBackgroundResource(R.drawable.lijia_off);
            mLeavingHomeWarningIv.setVisibility(View.VISIBLE);
            mLeavingHomeDevicesTv.setText("Not Available");
        }
        if (mSoWaring == true) {
            mSleepModeIv.setBackgroundResource(R.drawable.shape_bg_background_waring);
            mSleepIv.setBackgroundResource(R.drawable.sleep_off);
            mSleepModeWarningIv.setVisibility(View.VISIBLE);
            mSleepModeDevicesTv.setText("Not Available");

        }
        if (mSoWaring == true || mHuWaring == true) {
            mEnergySavingIV.setBackgroundResource(R.drawable.shape_bg_background_waring);
            mEnergySavingIv.setBackgroundResource(R.drawable.jieneng_off);
            mEnergySavingDevicesTv.setText("Not Available");
            mLocationIv.setVisibility(View.VISIBLE);
        }




    }


    public PatternFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_content2, container, false);
        cartoonComingHomeIv = view.findViewById(R.id.cartoon_coming_home_IV);
        mCartoonLeavingHomeIv = view.findViewById(R.id.cartoon_leaving_home_Iv);
        mCartoonSleepModeIv = view.findViewById(R.id.cartoon_sleep_mode_Iv);
        mCartoonEnergySavingIv = view.findViewById(R.id.cartoon_energy_saving);
        ComingHomeIv = view.findViewById(R.id.coming_home_IV);
        mLeavingHomeIv = view.findViewById(R.id.leaving_home_IV);
        mSleepModeIv = view.findViewById(R.id.sleep_mode_IV);
        mEnergySavingIV = view.findViewById(R.id.energy_saving_IV);
        mSleepModeDevicesTv = view.findViewById(R.id.sleep_mode_devices_Tv);
        mComingHomeDevicesTv = view.findViewById(R.id.coming_home_devices_Tv);
        mLeavingHomeDevicesTv = view.findViewById(R.id.leaving_home_devices_Tv);
        mEnergySavingDevicesTv = view.findViewById(R.id.energy_saving_devices_Tv);
        mSleepIv = view.findViewById(R.id.sleepIv);
        mComingHomeIv = view.findViewById(R.id.coming_home_Iv2);
        mLeavingHomeIV = view.findViewById(R.id.leaving_homeIV);
        mEnergySavingIv = view.findViewById(R.id.energy_savingIv2);
        mComingHomeWaringIv = view.findViewById(R.id.coming_home_waring_Iv);
        mLeavingHomeWarningIv =view.findViewById(R.id.leaving_home_warning_Iv);
        mLocationIv = view.findViewById(R.id.energy_saving_warning_Iv);
        mSleepModeWarningIv = view.findViewById(R.id.sleep_mode_warning_Iv);
        context = getContext();

        final int originalWidth1 = 500; // 图片的原始宽度
        // 设置图片的初始宽度为压缩状态
        ViewGroup.LayoutParams params1 = cartoonComingHomeIv.getLayoutParams();
        params1.width = originalWidth1 / 500;
        cartoonComingHomeIv.setLayoutParams(params1);

        // 第一步：使图片可见并横向拉伸到原始大小
        final int originalWidth2 = 500; // 图片的原始宽度
        // 设置图片的初始宽度为压缩状态
        ViewGroup.LayoutParams params2 = mCartoonLeavingHomeIv.getLayoutParams();
        params2.width = originalWidth2 / 500; // 假设初始宽度是原始宽度的110分之1
        mCartoonLeavingHomeIv.setLayoutParams(params2);

        final int originalWidth3 = 500; // 图片的原始宽度
        // 设置图片的初始宽度为压缩状态
        ViewGroup.LayoutParams params3 = mCartoonSleepModeIv.getLayoutParams();
        params3.width = originalWidth3 / 500;
        mCartoonSleepModeIv.setLayoutParams(params3);

        final int originalWidth4 = 500; // 图片的原始宽度
        // 设置图片的初始宽度为压缩状态
        ViewGroup.LayoutParams params4 = mCartoonEnergySavingIv.getLayoutParams();
        params4.width = originalWidth4 / 500;
        mCartoonEnergySavingIv.setLayoutParams(params4);

        mComingHomeWaringIv.setVisibility(View.GONE);
        mLeavingHomeWarningIv.setVisibility(View.GONE);
        mSleepModeWarningIv.setVisibility(View.GONE);
        mLocationIv.setVisibility(View.GONE);

        prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String key) {
                updateDeviceCount();
            }
        };
        // 注册监听器
        SharedPreferences sp = getActivity().getSharedPreferences("deviceInfo", MODE_PRIVATE);
        sp.registerOnSharedPreferenceChangeListener(prefListener);
        updateDeviceCount();


        ComingHomeIv.setOnClickListener(new View.OnClickListener() {
            private boolean isVisible = true;


            @SuppressLint("ResourceAsColor")
            @Override
            public void onClick(View v) {

                if (mFlagComingHome == 1) {
                    String show = "设备";
                    if (mAcWaring == true || mSoWaring == true || mApWaring == true || mHuWaring == true) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);
                        if (mAcWaring == true) {
                            show += "空调,";
                        }
                        if (mSoWaring == true) {
                            show += "开关,";
                        }
                        if (mApWaring == true) {
                            show += "空气进化器,";
                        }
                        if (mHuWaring == true) {
                            show += "加湿器";
                        }

                        builder.setTitle(show + "已损坏\n" + "是否进行修理");
                        builder.setPositiveButton("立即修理", ((dialog, which) -> {
                            mComingHomeWaringIv.setVisibility(View.GONE);
                            mLocationIv.setVisibility(View.GONE);
                            mLeavingHomeWarningIv.setVisibility(View.GONE);
                            mSleepModeWarningIv.setVisibility(View.GONE);
                            mAcWaring = false;
                            mSoWaring = false;
                            mApWaring = false;
                            mHuWaring = false;
                            saveSP1();
                        }));
                        builder.setNegativeButton("稍等片刻", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.getWindow().setBackgroundDrawableResource(R.color.gold);
                        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
                        params.copyFrom(dialog.getWindow().getAttributes());
                        params.width = 600;
                        params.height = 400;

                        dialog.getWindow().setAttributes(params);
                        dialog.show();
                    } else {
                        if (!mIsOpen) {
                            ComingHomeIv.setBackgroundResource(R.drawable.shape_bg_background);
                            mComingHomeIv.setBackgroundResource(R.drawable.zaijia_off);
                            mLeavingHomeIv.setEnabled(true);
                            mIsOpen = true;
                        } else {
                            //节能模式设置互斥事件
                            mEnergySavingIV.setEnabled(false);
                            //离家模式
                            mLeavingHomeIv.setEnabled(false);
                            //睡眠模式设置互斥事件
                            mSleepModeIv.setEnabled(false);
                            //睡眠模式开启时，将关闭
                            mSleepModeIv.setBackgroundResource(R.drawable.shape_bg_background);
                            mSleepIv.setBackgroundResource(R.drawable.sleep_off);
                            //节能模式开启时，将关闭
                            mEnergySavingIV.setBackgroundResource(R.drawable.shape_bg_background);
                            mEnergySavingIv.setBackgroundResource(R.drawable.jieneng_off);

                            saveSP1();
                            mFlagComingHome = 0;
                            ComingHomeIv.setBackgroundResource(R.drawable.shape_bg_background2);
                            mComingHomeIv.setBackgroundResource(R.drawable.zaijia_on);
                            // 设置图片的初始宽度为压缩状态
                            ViewGroup.LayoutParams params1 = cartoonComingHomeIv.getLayoutParams();
                            params1.width = originalWidth1 / 500;
                            cartoonComingHomeIv.setLayoutParams(params1);
                            // 如果ImageView22当前是隐藏的，则先显示它
                            if (!isVisible) {
                                cartoonComingHomeIv.setVisibility(View.VISIBLE);
                                isVisible = true;
                            }
                            // 动画：横向拉伸到原始大小
                            ValueAnimator widthAnimator = ValueAnimator.ofInt(params1.width, originalWidth1);
                            widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                                @Override
                                public void onAnimationUpdate(ValueAnimator animation) {
                                    params1.width = (int) animation.getAnimatedValue();
                                    cartoonComingHomeIv.setLayoutParams(params1);

                                }
                            });
                            widthAnimator.setDuration(1200); // 动画时长
                            widthAnimator.start();

                            // 第二步：向右移动一段距离
                            // 动画：平移
                            ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(cartoonComingHomeIv, "translationX", 0, 600); // 向右移动
                            // 可以将多个动画组合成一个AnimatorSet来同时播放
                            AnimatorSet animatorSet = new AnimatorSet();
                            animatorSet.playTogether(scaleXAnimator);
                            animatorSet.setDuration(1800);
                            // 添加动画监听器以在动画结束时执行操作
                            animatorSet.addListener(new AnimatorListenerAdapter() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    super.onAnimationEnd(animation);
                                    // 动画结束时将ImageView22的可见性设置为GONE，使其从布局中消失
                                    cartoonComingHomeIv.setVisibility(View.VISIBLE);
                                    isVisible = false;
                                    mFlagComingHome = 1;

                                    mEnergySavingIV.setEnabled(true);
                                    mSleepModeIv.setEnabled(true);

                                }
                            });
                            animatorSet.start();
                            mIsOpen = false;

                        }
                    }
                }
            }
        });
        mLeavingHomeIv.setOnClickListener(new View.OnClickListener() {
            private boolean isVisible = true; // 用于跟踪ImageView的可见性
//            private boolean isopen = true;

            @Override
            public void onClick(View view) {
                if (mFlagLeavingHome == 1) {
                    String show = "设备";
                    if (mSoWaring == true) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);

                        if (mSoWaring == true) {
                            show += "开关,";
                        }
                        builder.setTitle(show + "已损坏\n" + "是否进行修理");
                        builder.setPositiveButton("立即修理", ((dialog, which) -> {
                            mSoWaring = false;
                            mLeavingHomeWarningIv.setVisibility(View.GONE);
                            mSleepModeWarningIv.setVisibility(View.GONE);
                            saveSP2();
                        }));
                        builder.setNegativeButton("稍等片刻", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.getWindow().setBackgroundDrawableResource(R.color.gold);
                        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
                        params.copyFrom(dialog.getWindow().getAttributes());
                        params.width = 600;
                        params.height = 400;

                        dialog.getWindow().setAttributes(params);
                        dialog.show();
                    } else if (!mIsOpen) {
                        mLeavingHomeIv.setBackgroundResource(R.drawable.shape_bg_background);
                        mLeavingHomeIV.setBackgroundResource(R.drawable.lijia_off);
                        mIsOpen = true;
                        ComingHomeIv.setEnabled(true);
                    } else {
                        //睡眠模式设置互斥事件
                        mSleepModeIv.setEnabled(false);
                        //回家模式设置互斥事件
                        ComingHomeIv.setEnabled(false);
                        //节能模式设置互斥事件
                        mEnergySavingIV.setEnabled(false);
                        //睡眠模式开启时，将关闭
                        mSleepModeIv.setBackgroundResource(R.drawable.shape_bg_background);
                        mSleepIv.setBackgroundResource(R.drawable.sleep_off);
                        //节能模式开启时，将关闭
                        mEnergySavingIV.setBackgroundResource(R.drawable.shape_bg_background);
                        mEnergySavingIv.setBackgroundResource(R.drawable.jieneng_off);
                        saveSP2();
                        mFlagLeavingHome = 0;
                        mLeavingHomeIv.setBackgroundResource(R.drawable.shape_bg_background2);
                        mLeavingHomeIV.setBackgroundResource(R.drawable.lijia_on);
                        // 设置图片的初始宽度为压缩状态
                        ViewGroup.LayoutParams params2 = mCartoonLeavingHomeIv.getLayoutParams();
                        params2.width = originalWidth2 / 500;
                        mCartoonLeavingHomeIv.setLayoutParams(params2);
                        // 如果ImageView23当前是隐藏的，则先显示它
                        if (!isVisible) {
                            mCartoonLeavingHomeIv.setVisibility(View.VISIBLE);
                            isVisible = true;
                        }
                        // 动画：横向拉伸到原始大小
                        ValueAnimator widthAnimator = ValueAnimator.ofInt(params2.width, originalWidth2);
                        widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator animation) {
                                params2.width = (int) animation.getAnimatedValue();
                                mCartoonLeavingHomeIv.setLayoutParams(params2);
                            }
                        });
                        widthAnimator.setDuration(1200); // 动画时长
                        widthAnimator.start();

                        // 第二步：向右移动一段距离
                        // 动画：平移
                        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(mCartoonLeavingHomeIv, "translationX", 0, 600); // 向右移动
                        // 可以将多个动画组合成一个AnimatorSet来同时播放
                        AnimatorSet animatorSet = new AnimatorSet();
                        animatorSet.playTogether(scaleXAnimator);
                        animatorSet.setDuration(1800);
                        // 添加动画监听器以在动画结束时执行操作
                        animatorSet.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                super.onAnimationEnd(animation);
                                // 动画结束时将ImageView23的可见性设置为GONE，使其从布局中消失
                                mCartoonLeavingHomeIv.setVisibility(View.GONE);
                                isVisible = false;
                                mFlagLeavingHome = 1;

                                mEnergySavingIV.setEnabled(true);
                                mSleepModeIv.setEnabled(true);
                                //  isopen = true;
                            }
                        });
                        animatorSet.start();
                        mIsOpen = false;
                    }
                }
            }
        });
        mSleepModeIv.setOnClickListener(new View.OnClickListener() {
            private boolean isVisible = true; // 用于跟踪ImageView的可见性
            //            private boolean isopen = true;
            @Override
            public void onClick(View view) {

                if (mFlagSleepMode == 1) {
                    String show = "设备";
                    if (mSoWaring == true) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);

                        if (mSoWaring == true) {
                            show += "开关,";
                        }
                        builder.setTitle(show + "已损坏\n" + "是否进行修理");
                        builder.setPositiveButton("立即修理", ((dialog, which) -> {
                            mSoWaring = false;
                            mSleepModeWarningIv.setVisibility(View.GONE);
                            mLeavingHomeWarningIv.setVisibility(View.GONE);
                            saveSP3();
                        }));
                        builder.setNegativeButton("稍等片刻", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.getWindow().setBackgroundDrawableResource(R.color.gold);
                        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
                        params.copyFrom(dialog.getWindow().getAttributes());
                        params.width = 600;
                        params.height = 400;

                        dialog.getWindow().setAttributes(params);
                        dialog.show();
                    } else if (!mIsOpen) {
                        mSleepModeIv.setBackgroundResource(R.drawable.shape_bg_background);
                        mSleepIv.setBackgroundResource(R.drawable.sleep_off);
                        mIsOpen = true;
                    } else {
                        //回家模式设置互斥事件
                        ComingHomeIv.setEnabled(false);
                        //离家模式设置互斥事件
                        mLeavingHomeIv.setEnabled(false);
                        //节能模式设置互斥事件
                        mEnergySavingIV.setEnabled(false);
                        //节能模式开启时，将关闭
                        mEnergySavingIV.setBackgroundResource(R.drawable.shape_bg_background);
                        mEnergySavingIv.setBackgroundResource(R.drawable.jieneng_off);
                        //离家模式开启时，将关闭
                        mLeavingHomeIv.setBackgroundResource(R.drawable.shape_bg_background);
                        mLeavingHomeIV.setBackgroundResource(R.drawable.lijia_off);
                        //回家模式开启时，将关闭
                        ComingHomeIv.setBackgroundResource(R.drawable.shape_bg_background);
                        mComingHomeIv.setBackgroundResource(R.drawable.zaijia_off);

                        saveSP3();
                        mFlagSleepMode = 0;
                        mSleepIv.setBackgroundResource(R.drawable.sleep_on);
                        mSleepModeIv.setBackgroundResource(R.drawable.shape_bg_background2);

                        // 设置图片的初始宽度为压缩状态
                        ViewGroup.LayoutParams params3 = mCartoonSleepModeIv.getLayoutParams();
                        params3.width = originalWidth3 / 500;
                        mCartoonSleepModeIv.setLayoutParams(params3);
                        // 如果ImageView24当前是隐藏的，则先显示它
                        if (!isVisible) {
                            mCartoonSleepModeIv.setVisibility(View.VISIBLE);
                            isVisible = true;
                        }
                        // 动画：横向拉伸到原始大小
                        ValueAnimator widthAnimator = ValueAnimator.ofInt(params3.width, originalWidth3);
                        widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator animation) {
                                params3.width = (int) animation.getAnimatedValue();
                                mCartoonSleepModeIv.setLayoutParams(params3);
                            }
                        });
                        widthAnimator.setDuration(1450); // 动画时长
                        widthAnimator.start();

                        // 第二步：向右移动一段距离
                        // 动画：平移
                        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(mCartoonSleepModeIv, "translationX", 0, 600); // 向右移动
                        // 可以将多个动画组合成一个AnimatorSet来同时播放
                        AnimatorSet animatorSet = new AnimatorSet();
                        animatorSet.playTogether(scaleXAnimator);
                        animatorSet.setDuration(1550);
                        // 添加动画监听器以在动画结束时执行操作
                        animatorSet.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                super.onAnimationEnd(animation);
                                // 动画结束时将ImageView24的可见性设置为GONE，使其从布局中消失
                                mCartoonSleepModeIv.setVisibility(View.GONE);
                                isVisible = false;
                                mFlagSleepMode = 1;

                                ComingHomeIv.setEnabled(true);
                                mLeavingHomeIv.setEnabled(true);
                                mEnergySavingIV.setEnabled(true);


                            }
                        });
                        animatorSet.start();
                        mIsOpen = false;
                    }
                }
            }
        });
        mEnergySavingIV.setOnClickListener(new View.OnClickListener() {
            private boolean isVisible = true; // 用于跟踪ImageView的可见性
            //     private boolean isopen = true;

            @Override
            public void onClick(View view) {
                if (mFlagEnergySaving == 1) {
                    String show = "设备";
                    if ( mSoWaring == true || mHuWaring == true) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);

                        if (mSoWaring == true) {
                            show += "开关,";
                        }
                        if (mHuWaring == true) {
                            show += "加湿器";
                        }
                        builder.setTitle(show + "已损坏\n" + "是否进行修理");
                        builder.setPositiveButton("立即修理", ((dialog, which) -> {
                            mSoWaring = false;
                            mHuWaring = false;
                            mLocationIv.setVisibility(View.GONE);
                            mLeavingHomeWarningIv.setVisibility(View.GONE);
                            mSleepModeWarningIv.setVisibility(View.GONE);
                            saveSP4();
                        }));
                        builder.setNegativeButton("稍等片刻", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.getWindow().setBackgroundDrawableResource(R.color.gold);
                        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
                        params.copyFrom(dialog.getWindow().getAttributes());
                        params.width = 600;
                        params.height = 400;

                        dialog.getWindow().setAttributes(params);
                        dialog.show();
                    } else if (!mIsOpen) {
                        mEnergySavingIV.setBackgroundResource(R.drawable.shape_bg_background);
                        mEnergySavingIv.setBackgroundResource(R.drawable.jieneng_off);
                        mIsOpen = true;
                    } else {
                        //回家模式设置互斥事件
                        ComingHomeIv.setEnabled(false);
                        //离家模式设置互斥事件
                        mLeavingHomeIv.setEnabled(false);
                        //睡眠模式设置互斥事件
                        mSleepModeIv.setEnabled(false);
                        //睡眠模式启动时，将关闭
                        mSleepModeIv.setBackgroundResource(R.drawable.shape_bg_background);
                        mSleepIv.setBackgroundResource(R.drawable.sleep_off);
                        //离家模式开启时，将关闭
                        mLeavingHomeIv.setBackgroundResource(R.drawable.shape_bg_background);
                        mLeavingHomeIV.setBackgroundResource(R.drawable.lijia_off);
                        //回家模式开启时，将关闭
                        ComingHomeIv.setBackgroundResource(R.drawable.shape_bg_background);
                        mComingHomeIv.setBackgroundResource(R.drawable.zaijia_off);
                        saveSP4();
                        mFlagEnergySaving = 0;
                        mEnergySavingIv.setBackgroundResource(R.drawable.jieneng_on);
                        mEnergySavingIV.setBackgroundResource(R.drawable.shape_bg_background2);
                        // 设置图片的初始宽度为压缩状态
                        ViewGroup.LayoutParams params3 = mCartoonEnergySavingIv.getLayoutParams();
                        params3.width = originalWidth4 / 500;
                        mCartoonEnergySavingIv.setLayoutParams(params3);
                        // 如果ImageView24当前是隐藏的，则先显示它
                        if (!isVisible) {
                            mCartoonEnergySavingIv.setVisibility(View.VISIBLE);
                            isVisible = true;
                        }
                        // 动画：横向拉伸到原始大小
                        ValueAnimator widthAnimator = ValueAnimator.ofInt(params3.width, originalWidth4);
                        widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator animation) {
                                params3.width = (int) animation.getAnimatedValue();
                                mCartoonEnergySavingIv.setLayoutParams(params3);
                            }
                        });
                        widthAnimator.setDuration(1200); // 动画时长
                        widthAnimator.start();

                        // 第二步：向右移动一段距离
                        // 动画：平移
                        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(mCartoonEnergySavingIv, "translationX", 0, 600); // 向右移动
                        // 可以将多个动画组合成一个AnimatorSet来同时播放
                        AnimatorSet animatorSet = new AnimatorSet();
                        animatorSet.playTogether(scaleXAnimator);
                        animatorSet.setDuration(1800);
                        // 添加动画监听器以在动画结束时执行操作
                        animatorSet.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                super.onAnimationEnd(animation);
                                // 动画结束时将ImageView24的可见性设置为GONE，使其从布局中消失
                                mCartoonEnergySavingIv.setVisibility(View.GONE);
                                mFlagEnergySaving = 1;
                                isVisible = false;

                                ComingHomeIv.setEnabled(true);
                                mLeavingHomeIv.setEnabled(true);
                                mSleepModeIv.setEnabled(true);
//                                mTemp = 1;
                            }
                        });
                        animatorSet.start();
                        mIsOpen = false;
                    }
                }
            }
        });
        return view;
    }

    private void saveSP1() {
        if (isAdded() && getActivity() != null) {
            SharedPreferences sharedPreferences = getActivity().getSharedPreferences("deviceInfo", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            mAcState = true;
            mSoState = true;
            mApState = true;
            mHuState = true;
            editor.putBoolean("acState", mAcState);
            editor.putBoolean("soState", mSoState);
            editor.putBoolean("apState", mApState);
            editor.putBoolean("huState", mHuState);//sw为真或假
            editor.putBoolean("acWaring", mAcWaring);
            editor.putBoolean("mSoWaring", mSoWaring);
            editor.putBoolean("mApWaring", mApWaring);
            editor.putBoolean("mHuWaring", mHuWaring);
            editor.putInt("temp", 1);
            editor.commit();

        } else {
            Log.w("MyFragment1", "Fragment is not attached to an activity");
        }
    }

    private void saveSP2() {
        if (isAdded() && getActivity() != null) {
            SharedPreferences sharedPreferences = getActivity().getSharedPreferences("deviceInfo", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            mAcState = false;
            mSoState = true;
            mApState = false;
            mHuState = false;
            editor.putBoolean("acState", mAcState);
            editor.putBoolean("soState", mSoState);
            editor.putBoolean("apState", mApState);
            editor.putBoolean("huState", mHuState);//sw为真或假
            editor.putBoolean("mSoWaring", mSoWaring);
            editor.putInt("temp", 1);
            editor.commit();
        } else {
            Log.w("MyFragment1", "Fragment is not attached to an activity");
        }
    }

    private void saveSP3() {
        if (isAdded() && getActivity() != null) {
            SharedPreferences sharedPreferences = getActivity().getSharedPreferences("deviceInfo", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            mAcState = false;
            mSoState = false;
            mApState = false;
            mHuState = false;
            editor.putBoolean("acState", mAcState);
            editor.putBoolean("soState", mSoState);
            editor.putBoolean("apState", mApState);
            editor.putBoolean("huState", mHuState);//sw为真或假
            editor.putBoolean("mSoWaring", mSoWaring);
            editor.putInt("temp", 1);
            editor.commit();
        } else {
            Log.w("MyFragment1", "Fragment is not attached to an activity");
        }
    }

    private void saveSP4() {
        if (isAdded() && getActivity() != null) {
            SharedPreferences sharedPreferences = getActivity().getSharedPreferences("deviceInfo", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            mAcState = false;
            mSoState = true;
            mApState = false;
            mHuState = true;
            editor.putBoolean("acState", mAcState);
            editor.putBoolean("soState", mSoState);
            editor.putBoolean("apState", mApState);
            editor.putBoolean("huState", mHuState);//sw为真或假
            editor.putBoolean("mSoWaring", mSoWaring);
            editor.putBoolean("mHuWaring", mHuWaring);
            editor.putInt("temp", 1);
            editor.commit();
        } else {
            Log.w("MyFragment1", "Fragment is not attached to an activity");
        }
    }
}
